package com.cloud.system.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class IdentityNOHelper {
	// 省直辖市代码
	private static String[] cityCode = { "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35",
			"36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65",
			"71", "81", "82", "91" };
	// 每位上的加权因子
	private static int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

	/**
	 * 校验证件号码是否有效
	 * 
	 * @param idCard
	 * @return
	 */
	public static boolean isValid(String idCard) {
		if (StringUtils.isEmpty(idCard) || (idCard.length() != 15 && idCard.length() != 18)) {
			return false;
		} else {
			if (idCard.length() == 15) {
				return validate15IdNum(idCard);
			} else {
				return validate18IdNum(idCard);
			}
		}
	}
	
	/**
	 * 获取身份证出生日期
	 * 
	 * @param idNum
	 * @return
	 */
	public static Date getBirthDay(String idCard){
		
		return null;
	}
	
	/**
	 * 获取省份证性别
	 * @param idNum
	 * @return
	 */
	public static int getGender(String idCard){
		return 0;
	}

	private static boolean validate18IdNum(String idNum) {
		String idcard17 = idNum.substring(0, 17);
		if (!isDigital(idcard17)) {
			return false;
		}
		String provinceid = idNum.substring(0, 2);
		// 校验省份
		if (!checkProvince(provinceid)) {
			return false;
		}
		// 校验出生日期
		String birthday = idNum.substring(6, 14);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		try {
			Date birthDate = sdf.parse(birthday);
			String tmpDate = sdf.format(birthDate);
			// 出生年月日不正确
			if (!tmpDate.equals(birthday)) {
				return false;
			}
		} catch (ParseException e1) {
			return false;
		}
		// 获取第18位
		String idcard18Code = idNum.substring(17, 18);
		char c[] = idcard17.toCharArray();
		int bit[] = converCharToInt(c);
		int sum17 = 0;
		sum17 = getPowerSum(bit);
		// 将和值与11取模得到余数进行校验码判断
		String checkCode = getCheckCodeBySum(sum17);
		if (null == checkCode) {
			return false;
		}
		// 匹配校验码
		if (!idcard18Code.equalsIgnoreCase(checkCode)) {
			return false;
		}
		return true;
	}

	/**
	 * 将和值与11取模得到余数进行校验码判断
	 * 
	 * @param checkCode
	 * @param sum17
	 * @return 校验位
	 */
	private static String getCheckCodeBySum(int sum17) {
		String checkCode = null;
		switch (sum17 % 11) {
		case 10:
			checkCode = "2";
			break;
		case 9:
			checkCode = "3";
			break;
		case 8:
			checkCode = "4";
			break;
		case 7:
			checkCode = "5";
			break;
		case 6:
			checkCode = "6";
			break;
		case 5:
			checkCode = "7";
			break;
		case 4:
			checkCode = "8";
			break;
		case 3:
			checkCode = "9";
			break;
		case 2:
			checkCode = "x";
			break;
		case 1:
			checkCode = "0";
			break;
		case 0:
			checkCode = "1";
			break;
		}
		return checkCode;
	}

	/**
	 * 将身份证的每位和对应位的加权因子相乘之后，再得到和值
	 * 
	 * @param bit
	 * @return
	 */
	private static int getPowerSum(int[] bit) {
		int sum = 0;
		if (power.length != bit.length) {
			return sum;
		}
		for (int i = 0; i < bit.length; i++) {
			for (int j = 0; j < power.length; j++) {
				if (i == j) {
					sum = sum + bit[i] * power[j];
				}
			}
		}
		return sum;

	}

	/**
	 * 将字符数组转为整型数组
	 * 
	 * @param c
	 * @return
	 * @throws NumberFormatException
	 */
	private static int[] converCharToInt(char[] c) {
		int[] a = new int[c.length];
		int k = 0;
		for (char temp : c) {
			a[k++] = Integer.parseInt(String.valueOf(temp));
		}
		return a;

	}

	/**
	 * 数字校验
	 * @param cardNum
	 * @return
	 */
	private static boolean isDigital(String cardNum) {
		return cardNum.matches("^[0-9]*$");
	}

	/**
	 * 校验省份
	 * 
	 * @param provinceid
	 * @return 合法返回TRUE，否则返回FALSE
	 */
	private static boolean checkProvince(String provinceid) {
		for (String id : cityCode) {
			if (id.equals(provinceid)) {
				return true;
			}
		}
		return false;
	}

	private static boolean validate15IdNum(String idcard) {
       // 15全部为数字 
       if (!isDigital(idcard)) { 
           return false; 
       } 
       String province = idcard.substring(0, 2); 
       // 校验省份 
       if (!checkProvince(province)) { 
           return false; 
       } 
       String birthday = idcard.substring(6, 12); 
       SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd"); 
       try { 
           Date birthDate = sdf.parse(birthday); 
           String tmpDate = sdf.format(birthDate); 
           if (!tmpDate.equals(birthday)) {// 身份证日期错误 
               return false; 
           } 
       } catch (ParseException e1) { 
           return false; 
       } 
       return true; 
	}

}
